package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.jsoup.Jsoup;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;

import entities.Domain;

public class S3QueueHandle {
	
	public static String queueServer = "http://54.83.253.204:1218/";
	public static String queueName = "buildwith";
	public static String queueErrorName = "buildwith_error";

	// put error data to queue
	public static boolean putErrorQueue(String data) {
		boolean success = false;
		try {
			String action = "put";
			String url = queueServer + "?name=" + queueErrorName + "&opt="
					+ action + "&data=" + data;
			Jsoup.connect(url).execute();
			success = true;
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
		}
		return success;
	}

	// return domain name from queue
	public static String pullQueue() {
		try {
			String action = "get";
			URL obj = new URL(queueServer + "?name=" + queueName + "&opt="
					+ action);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
			con.setRequestMethod("GET");
			con.setRequestProperty("User-Agent", "Mozilla/5.0");
			BufferedReader in = new BufferedReader(new InputStreamReader(
					con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
			return response.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	// return domain object with index and header information
	public static Domain getDomainInformationFromS3(String domainName)
			throws IOException {
		Domain domain = new Domain();
		AmazonS3 s3 = new AmazonS3Client(
				new ClasspathPropertiesFileCredentialsProvider());
		Region usWest2 = Region.getRegion(Regions.US_EAST_1);
		s3.setRegion(usWest2);

		String bucketName = "domain-list";
		try {
			// get header
			String key = "1m/" + domainName + "/header.html";
			S3Object object = s3
					.getObject(new GetObjectRequest(bucketName, key));
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					object.getObjectContent()));

			StringBuffer header = new StringBuffer();
			while (true) {
				String line = reader.readLine();
				if (line == null)
					break;
				else {
					header.append("    " + line + "\n");
				}
			}
			domain.setHeader(header.toString());

			// get index
			key = "1m/" + domainName + "/index.html";
			object = s3.getObject(new GetObjectRequest(bucketName, key));
			reader = new BufferedReader(new InputStreamReader(
					object.getObjectContent()));
			StringBuffer html = new StringBuffer();
			while (true) {
				String line = reader.readLine();
				if (line == null)
					break;
				else {
					html.append(line + "\n");
				}
			}
			domain.setHtml(html.toString());
		} catch (AmazonServiceException ase) {
			ase.printStackTrace();
			return null;
		} catch (AmazonClientException ace) {
			ace.printStackTrace();
			return null;
		}
		return domain;
	}

	public static Domain getDomain() throws IOException {
		return getDomainInformationFromS3(pullQueue());
	}
}
